/**
 * 
 */
package com.guohong.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.guohong.constant.MaodouConstant;
import com.guohong.dto.ElTreeDto;
import com.guohong.entity.Menu;
import com.guohong.entity.Permission;
import com.guohong.mapper.MenuMapper;
import com.guohong.mapper.PermissionMapper;
import com.guohong.service.MenuService;
import com.guohong.vo.MenuVo;

/**
 * @author mao_g
 *
 */
@Service
public class MenuServiceImpl implements MenuService {
	@Autowired
	private MenuMapper menuMapper;
	@Autowired
	private PermissionMapper permissionMapper;

	@Override
	public Menu addMenuAndPermissions(Menu menu, List<Permission> permissions) {

		return null;
	}

	@Override
	public List<Menu> getMenuAndPermissionsTree() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public ElTreeDto getMenuTree() {
		ElTreeDto ROOT = new ElTreeDto();
		ROOT.setId(MaodouConstant.MENU_ROOT_ID);

		recursion(ROOT);

		return ROOT;
	}

	// ======
	// TODO
	private void recursion(ElTreeDto parentMenu) {
		List<ElTreeDto> children = new ArrayList<>();
		
		// 设置权限，通过menu_id获取权限
		List<Permission> permissions = permissionMapper.selectByMenuId(parentMenu.getId());
		if (!CollectionUtils.isEmpty(permissions)) {
			for (Permission permission : permissions) {
				ElTreeDto elTreeDto = new ElTreeDto();
				elTreeDto.setId(permission.getId());
				elTreeDto.setLabel(permission.getPermissionName());
				elTreeDto.setIsMenu(false);
				elTreeDto.setParentMenuId(parentMenu.getId());
				elTreeDto.setKey(parentMenu.getId()+"_"+permission.getId());
				children.add(elTreeDto);
			}
		}
		
		List<Menu> subMenus = getSubMenus(parentMenu.getId());
		if (!CollectionUtils.isEmpty(subMenus)) { // 有子菜单
			for (Menu menu : subMenus) {
				ElTreeDto elTreeDto = new ElTreeDto();
				elTreeDto.setId(menu.getId());
				elTreeDto.setLabel(menu.getName());
				elTreeDto.setIsMenu(true);
				elTreeDto.setParentMenuId(parentMenu.getId());
				elTreeDto.setKey(menu.getId().toString());
				children.add(elTreeDto);
				recursion(elTreeDto);
			}

//			parentMenu.setChildren(children);
		}
		
		if (children.size() > 0) {
			parentMenu.setChildren(children);
		}
	}

	private List<Menu> getSubMenus(Long parentId) {
		return menuMapper.getSubMenus(parentId);
	}

	@Override
	public ElTreeDto getMenuOnlyTree() {
		ElTreeDto ROOT = new ElTreeDto();
		ROOT.setId(MaodouConstant.MENU_ROOT_ID);

		recursion2(ROOT);

		return ROOT;
	}

	private void recursion2(ElTreeDto parentMenu) {
		List<ElTreeDto> children = new ArrayList<>();

		List<Menu> subMenus = getSubMenus(parentMenu.getId());
		if (!CollectionUtils.isEmpty(subMenus)) { // 有子菜单
			for (Menu menu : subMenus) {
				ElTreeDto elTreeDto = new ElTreeDto();
				elTreeDto.setId(menu.getId());
				elTreeDto.setLabel(menu.getName());
				elTreeDto.setIsMenu(true);
				children.add(elTreeDto);
				recursion2(elTreeDto);
			}
		}

		if (children.size() > 0) {
			parentMenu.setChildren(children);
		}
	}

	@Transactional
	@Override
	public int insert(MenuVo menuVo) {
		Menu menu = new Menu();
		BeanUtils.copyProperties(menuVo, menu);
		menu.setCreateUser("admin");
		menu.setCreateTime(new Date());

		return menuMapper.insert(menu);
	}
}
